package com.gsi.webstore.platform.storefront.pipelet;

import java.util.HashMap;
import java.util.Map;

import ch.qos.logback.classic.Logger;

import com.intershop.beehive.core.capi.domain.Domain;
import com.intershop.beehive.core.capi.naming.NamingMgr;
import com.intershop.beehive.core.capi.pipeline.Pipelet;
import com.intershop.beehive.core.capi.pipeline.PipeletExecutionException;
import com.intershop.beehive.core.capi.pipeline.PipelineDictionary;
import com.intershop.beehive.xcs.capi.product.Product;
import com.intershop.beehive.xcs.capi.product.ProductMgr;

/**
 * 
 */
public class ValidatePreSellProduct extends Pipelet
{

    /**
     * Constant used to access the pipeline dictionary with key 'SKUItr'
     */
    public static final String DN_SKUITR = "SKUItr";
    /**
     * Constant used to access the pipeline dictionary with key 'PreSellMap'
     */
    public static final String DN_PRE_SELL_MAP = "PreSellMap";
    /**
     * Constant used to access the pipeline dictionary with key 'ProductDomain'
     */
    public static final String DN_PRODUCT_DOMAIN = "ProductDomain";

    /**
     * The pipelet's execute method is called whenever the pipelets gets
     * executed in the context of a pipeline and a request. The pipeline
     * dictionary valid for the currently executing thread is provided as a
     * parameter.
     * 
     * @param dict
     *            The pipeline dictionary to be used.
     * @throws PipeletExecutionException
     *             Thrown in case of severe errors that make the pipelet execute
     *             impossible (e.g. missing required input data).
     */
    public int execute(PipelineDictionary dict)
    {        
        // lookup 'ProductDomain' in pipeline dictionary
        Domain productDomain = (Domain)dict.get(DN_PRODUCT_DOMAIN);
        
        // lookup 'SKUItr' in pipeline dictionary
        java.util.Iterator<String> sKUItr = (java.util.Iterator<String>)dict.get(DN_SKUITR);
        if (null == productDomain)
        {
            try
            {
                throw new PipeletExecutionException("Mandatory input parameter 'productDomain' not available in pipeline dictionary.");
            }
            catch(PipeletExecutionException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        
        }
        if (null == sKUItr)
        {
            try
            {
                throw new PipeletExecutionException("Mandatory input parameter 'SKUItr' not available in pipeline dictionary.");
            }
            catch(PipeletExecutionException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        
        }
        HashMap<String, String> presellmap = new HashMap<String, String>();
        ProductMgr productMgr = (ProductMgr)NamingMgr.getInstance().lookupManager(ProductMgr.REGISTRY_NAME);
        Product product=null;
        String domainName=productDomain.getDomainName();
        String sku;
        while (sKUItr.hasNext())
        {
            sku=sKUItr.next();
            product=productMgr.getProductBySKU(sku,domainName);
           if (product.containsAttribute("FrontendAvailabilityStatusMessage_AdvancePreSell")){
               
               presellmap.put(product.getName(),"PreSellProduct");
           }
           
               
           }
        
        
        // store 'PreSellMap' in pipeline dictionary
        // TODO Put the value in dictionary
        dict.put(DN_PRE_SELL_MAP, presellmap);
        return PIPELET_NEXT;
    }
}